package close.mazegame.entity.mob;

import box2dLight.PointLight;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.math.Vector2;

import close.mazegame.art.tanks.TankTexture;
import close.mazegame.collision.Collidable;
import close.mazegame.entity.mob.AITank;
import close.mazegame.entity.mob.behaviour.FleeBehaviour;
import close.mazegame.entity.mob.behaviour.PatrolBehaviour;
import close.mazegame.entity.player.Player;
import close.mazegame.level.MazeLevel;
import close.mazegame.pathfinding.Pathfinder;

public class Fleer extends AITank {
	
	private static final float LIGHT_BRIGHTNESS = 0.8f;
	
	private PointLight pl;
	private float fleeTimer = 0.0f;

	public Fleer(Vector2 pos, MazeLevel level, Pathfinder pathfinder) {
		super(pos, level, TankTexture.black, pathfinder, false);
		pl = level.addPointLight(body, 64,  new Color(1.0f, 1.0f, 1.0f, LIGHT_BRIGHTNESS), 1.5f, 0);
		behaviour = new PatrolBehaviour(this);
		speed = 2;
		hasTurret = false;
	}
	
	public void update(float delta){
		super.update(delta);
		if(level.lineOfSightToPlayer(getPosition(), (float)Math.toDegrees(getAngle()), 40)){
			fleeTimer = 0;
			if(behaviour instanceof PatrolBehaviour){
				startFlee();
			}
		}
		fleeTimer += delta;
		if(fleeTimer > 10){
			fleeTimer = 0;
			startPatrol();
		}
	}
	
	private void startFlee() {
		behaviour = new FleeBehaviour(this);
		speed = BASE_SPEED;
		pl.setActive(false);
	}
	
	private void startPatrol() {
		behaviour = new PatrolBehaviour(this);
		speed = BASE_SPEED * 2.0f / 3.0f;
		pl.setActive(true);
	}
	
	public void dispose(){
		super.dispose();
		pl.remove();
	}

	@Override
	public void startContact(Collidable other) {
		super.startContact(other);
		if(other instanceof Player){
			startFlee();
		}
	}

	@Override
	public void endContact(Collidable other) {
	}
	
}
